//
//  LookInforViewController.m
//  SafeFoodManagerDemo
//
//  Created by bob on 2017/10/17.
//  Copyright © 2017年 bob. All rights reserved.
//

#import "LookInforViewController.h"
#import "LookInforTableViewCell.h"
#import "RSTableHeadView.h"
#import "LookInforModel.h"
#import "RegisterMorePicTableViewCell.h"
#import "RegisterSpicTableViewCell.h"
@interface LookInforViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)TB_TableView *homeTableView;

@property(nonatomic,strong)LookInforModel *inforModel;

@property(nonatomic,copy)NSString*headimage;

@end

@implementation LookInforViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.isScanphotos=YES;
    
    [self.view addSubview:self.homeTableView];
    
    [self loadDatas];
    
    // Do any additional setup after loading the view.
}

-(TB_TableView*)homeTableView
{
    if (!_homeTableView) {
        
        _homeTableView=[[TB_TableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-64) style:UITableViewStylePlain];
        
        _homeTableView.estimatedRowHeight=44;
        
        _homeTableView.separatorStyle=UITableViewCellSeparatorStyleNone;
        
        _homeTableView.delegate=self;
        
        _homeTableView.dataSource=self;
        
        [self configureNeedShowEmptyDataSetScrollView:_homeTableView];
        
        [_homeTableView registerNib:[UINib nibWithNibName:@"LookInforTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
        
        [_homeTableView registerClass:[RegisterMorePicTableViewCell class] forCellReuseIdentifier:@"TypeCell3"];
  
    
        [_homeTableView registerNib:[UINib nibWithNibName:@"RegisterSpicTableViewCell" bundle:nil] forCellReuseIdentifier:@"TypeCell5"];
        
         [_homeTableView registerClass:[RSTableHeadView class] forHeaderFooterViewReuseIdentifier:@"head"];
        
        _homeTableView.tableFooterView=[[UIView alloc]init];
        
        
    }
    
    return _homeTableView;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    return self.inforModel.data.count;
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    LookInforData *data=self.inforModel.data[section];
    
    return data.list.count;
    
}


-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LookInforData *data=self.inforModel.data[indexPath.section];
    
    LookInforList *list=data.list[indexPath.row];
    
    if (list.fieldType==4) {
        
        RegisterSpicTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"TypeCell5" forIndexPath:indexPath];
  
        cell.TitleName=list.name;
     
        if (list.value.length==0) {
            
            cell.IcoImageView.userInteractionEnabled=NO;
            
        }else{
            
            cell.IcoImageView.userInteractionEnabled=YES;
            
            self.headimage=list.value;
            
            UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(ShowHEAD)];
            
            [cell.IcoImageView addGestureRecognizer:tap];
            
        }
        
        [cell.IcoImageView LoadWebImageWithName:list.value andIsOrgin:NO];
        
        return cell;
 
    }else if (list.fieldType==5)
    {
        
        RegisterMorePicTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"TypeCell3" forIndexPath:indexPath];
     
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
  
        
        cell.TitleName=list.name;
       
        cell.isEdit=NO;//是否可编辑
        
        cell.ImageArr=[HelpWay imageStrToArr:list.value];//传递图片数组
   
          [cell updateLebeframe];
    
        TBWeak;
        //浏览图片
        cell.ScanBlock=^(NSInteger num)
        {
            [weakSelf.ScanPhotoArr removeAllObjects];
            
            for (NSString*imagestr in [HelpWay imageStrToArr:list.value]) {
                
                [weakSelf.ScanPhotoArr addObject:[MWPhoto photoWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/%@",ReqUrl,imagestr]]]];
                
            }
            
            [weakSelf GoToPhotoScanAndIndex:num];
            
        };
        
        return cell;
        
    }else{
    
        LookInforTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
        cell.list=list;
        
        return cell;
        
    }
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LookInforData *data=self.inforModel.data[indexPath.section];
    
    LookInforList *list=data.list[indexPath.row];
    
    if (list.fieldType==4) {
        
        return 60;
        
    }else if (list.fieldType==5)
    {
        NSArray *arr=[HelpWay imageStrToArr:list.value];
        
        if (arr.count==0) {
            
            return 43;
            
        }else{
        
        NSInteger colorm=(arr.count-1)/3+1;//每行放三个，一个有多少行
        
        CGFloat cellHeight=(SCREEN_WIDTH-30-10*2)/3.0;
        
        return 15+10*(colorm-1)+colorm*cellHeight+47;
            
        }
    }
    
    return UITableViewAutomaticDimension;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    
    return 30;
    
}


-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    LookInforData *data=self.inforModel.data[section];
    RSTableHeadView *head=[tableView dequeueReusableHeaderFooterViewWithIdentifier:@"head"];
    
    head.Rtitle=data.title;
    return head;
    
}

-(void)ShowHEAD
{
    [self.ScanPhotoArr removeAllObjects];
    
    [self.ScanPhotoArr addObject:[MWPhoto photoWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/%@",ReqUrl,self.headimage]]]];
    
    [self GoToPhotoScanAndIndex:0];
    
}

-(void)loadDatas
{
    [self showLoadingAnimation];
    
    BaseReqApi *api=[[BaseReqApi alloc]initWithRequestUrl:self.reqUrl andrequestTime:5 andParams:self.params andRequestMethod:YTKRequestMethodPOST andCache:NO andCacheTime:0 andPostToken:YES];
    
    [api StarRequest:^(TBResponseStatus responseStatus, NSString *message, id responseObject) {
        
        [self stopLoadingAnimation];
        
        
        
        self.inforModel=[LookInforModel mj_objectWithKeyValues:responseObject];
 
        if (responseStatus==1) {
            //请求成功
            
            NSLog(@"%@",[HelpWay dictionaryToJSON:responseObject]);
            if (self.inforModel.data.count==0) {

                self.Type=TBNODateType;

            }else{

                [self.homeTableView reloadData];

            }
        }else if (responseStatus==0||responseStatus==2)
        {
            [MBProgressHUD showErrorMessage:message];
            
            self.Type=TBReqFalie;
            
        }else{
            //404需要重新登录了
      
            [UIAlertView bk_showAlertViewWithTitle:message message:nil cancelButtonTitle:@"取消" otherButtonTitles:@[@"确定"] handler:^(UIAlertView *alertView, NSInteger buttonIndex) {
                
                if (buttonIndex==1) {
                    
                    [self GotoLoginVC];
                }
                
            }];
        }
 
    }];
    
}

- (void)actionTapEmptyDataSetNodataView
{
    [self loadDatas];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
